package se.thoka.twt.client.event;

import se.thoka.twt.client.utils.HasProgress;

import com.google.gwt.event.shared.GwtEvent;

public class CountDownEvent extends GwtEvent<CountDownHandler> implements HasProgress {
	
	public static final Type<CountDownHandler> TYPE = new Type<CountDownHandler>();
	
	private long timeLeft;
	private long totalTime;
	public CountDownEvent(long timeLeft, long totalTime) {
		if (totalTime <= 0) {
			throw new IllegalArgumentException("Total time must be greater than 0");
		}
		this.timeLeft = Math.max(0, Math.min(totalTime, timeLeft));
		this.totalTime = totalTime;
	}
	
	@Override
	public double getProgress() {
		return 1.0 * timeLeft / totalTime;
	}
	
	public long getTimeLeftInMillis() {
		return timeLeft;
	}
	
	public long getTotalTimeInMillis() {
		return totalTime;
	}
	
	public boolean isComplete() {
		return timeLeft == 0;
	}
	
	@Override
	public void setProgress(double progress) {}

	@Override
	public Type<CountDownHandler> getAssociatedType() {
		return TYPE;
	}

	@Override
	protected void dispatch(CountDownHandler handler) {
		handler.onCountDown(this);
	}

}
